﻿using Developer.Entity;
using Developer.Service;
using Super.Framework.Common;
using Super.Framework.Common.Web;
using Super.Framework.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Developer.Web.Areas.Admin.Controllers
{
    public class CycleController : BaseController
    {
        #region Type维修类型
        /// <summary>
        /// 首页列表/ 维修类型列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;

            var cities = CycleTypeService.GetAll().Where(a => a.IsAbort == false);
            if (key.IsNotNullOrEmpty())
            {
                cities = cities.Where(a => a.Name.Contains(key));
                ViewBag.Name = key;
            }
            var items = cities.OrderByDescending(u => u.CreateTime)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex).Select(a => new CycleType
                 {
                     Id = a.Id,
                     CreateTime = a.CreateTime,
                     Name = a.Name,
                     IsAbort = a.IsAbort,
                     CycleBrand = a.CycleBrand,
                 });

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
            return View(items);
        }


        public ActionResult EditType()
        {
            var typeId = Query.Value("typeId").ToInt();
            var CycleType = new CycleType();
            if (typeId > 0)
            {
                CycleType = CycleTypeService.GetAll().FirstOrDefault(c => c.Id == typeId);
            }
            return View(CycleType);
        }

        [HttpPost]
        public ActionResult EditType(int? z)
        {
            var typename = Query.Value("typename");
            var typeId = Query.Value("typeId").ToInt();
            var CycleType = new CycleType();
            if (typeId > 0)
            {
                CycleType = CycleTypeService.GetAll().FirstOrDefault(c => c.Id == typeId);
                if (CycleType != null)
                {
                    CycleType.Name = typename;
                    CycleTypeService.Change(CycleType);
                }
            }
            else
            {
                CycleType.Name = typename;
                CycleType.IsAbort = false;
                CycleType.CreateTime = DateTime.Now;
                CycleTypeService.Add(CycleType);
            }
            ShowMsg("操作成功", MessageItemType.Right);
            return Redirect("/admin/Cycle/edittype?typeId=" + typeId);
        }

        public ActionResult DeleteType()
        {
            var typeId = Query.Value("typeId").ToInt();
            if (typeId > 0)
            {
                CycleTypeService.Remove(typeId);
                ShowMsg("操作成功!", MessageItemType.Right);
                return Redirect("/admin/Cycle/type");
            }
            ShowMsg("操作失败,参数不正确!", MessageItemType.Error);
            return Redirect("/admin/Cycle/type");
        }
        #endregion

        #region Brand维修品牌
        public ActionResult Brand()
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;

            var cities = CycleBrandService.GetAll().Where(a => a.IsAbort == false);
            if (key.IsNotNullOrEmpty())
            {
                cities = cities.Where(a => a.Name.Contains(key));
                ViewBag.Name = key;
            }
            var items = cities.OrderByDescending(u => u.CreateTime)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex).Select(a => new CycleBrand
                 {
                     Id = a.Id,
                     CreateTime = a.CreateTime,
                     Name = a.Name,
                     IsAbort = a.IsAbort,
                     CycleTypeId = a.CycleTypeId,
                     CycleType = a.CycleType,
                     CycleModel = a.CycleModel
                 });

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
            return View(items);
        }


        public ActionResult EditBrand()
        {
            var typeId = Query.Value("typeId").ToInt();
            var CycleType = new CycleBrand();
            var remain = Query.Value("remain").ToInt();
            ViewBag.remain = remain;
            if (typeId > 0)
            {
                CycleType = CycleBrandService.GetAll().FirstOrDefault(c => c.Id == typeId);
            }
            return View(CycleType);
        }

        [HttpPost]
        public ActionResult EditBrand(int? z)
        {
            var typename = Query.Value("typename");
            var typeId = Query.Value("typeId").ToInt();
            var CycleParentId = Query.Value("parentId").ToInt();  //父类id
            var CycleType = new CycleBrand();
            if (typeId > 0)
            {
                CycleType = CycleBrandService.GetAll().FirstOrDefault(c => c.Id == typeId);
                if (CycleType != null)
                {
                    CycleType.Name = typename;
                    if (CycleParentId > 0)
                    {
                        CycleType.CycleTypeId = CycleParentId;
                    }
                    CycleBrandService.Change(CycleType);
                }
            }
            else
            {
                CycleType.Name = typename;
                CycleType.CreateTime = DateTime.Now;
                CycleType.IsAbort = false;
                CycleType.CycleTypeId = CycleParentId;

                var last_sequence = CycleBrandService.GetAll().Where(a => a.CycleTypeId == CycleParentId).OrderByDescending(a => a.Sequence);
                if(last_sequence.Count() > 0)
                {
                    CycleType.Sequence = last_sequence.FirstOrDefault().Sequence++;
                }
                else
                {
                    CycleType.Sequence = 1;
                }
                CycleBrandService.Add(CycleType);
            }
            ShowMsg("操作成功", MessageItemType.Right);
            return Redirect("/admin/Cycle/editbrand?typeId=" + typeId + "&remain="+CycleParentId);
        }

        public ActionResult DeleteBrand()
        {
            var typeId = Query.Value("typeId").ToInt();
            if (typeId > 0)
            {
                CycleBrandService.Remove(typeId);
                ShowMsg("操作成功!", MessageItemType.Right);
                return Redirect("/admin/Cycle/brand");
            }
            ShowMsg("操作失败,参数不正确!", MessageItemType.Error);
            return Redirect("/admin/Cycle/brand");
        }
        #endregion

        #region Model维修型号
        public ActionResult Model()
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;

            var cities = CycleModelService.GetAll().Where(a => a.IsAbort == false);
            if (key.IsNotNullOrEmpty())
            {
                cities = cities.Where(a => a.Name.Contains(key));
                ViewBag.Name = key;
            }
            var items = cities.OrderByDescending(u => u.CreateTime)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex).Select(a => new CycleModel
                 {
                     Id = a.Id,
                     CreateTime = a.CreateTime,
                     Name = a.Name,
                     IsAbort = a.IsAbort,
                     CycleBrandId = a.CycleBrandId,
                     CycleBrand = a.CycleBrand,
                     Price = a.Price,
                     CycleProblem = a.CycleProblem
                 });

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
            return View(items);
        }


        public ActionResult EditModel()
        {
            var typeId = Query.Value("typeId").ToInt();
            var CycleType = new CycleModel();
            var remain = Query.Value("remain").ToInt();
            ViewBag.remain = remain;
            if (typeId > 0)
            {
                CycleType = CycleModelService.GetAll().FirstOrDefault(c => c.Id == typeId);
            }
            return View(CycleType);
        }

        [HttpPost]
        public ActionResult EditModel(int? z)
        {
            var typename = Query.Value("typename");
            var typeId = Query.Value("typeId").ToInt();
            var price = Query.Value("price").ToInt();
            var CycleParentId = Query.Value("parentId").ToInt();  //父类id
            var CycleType = new CycleModel();
            if (typeId > 0)
            {
                CycleType = CycleModelService.GetAll().FirstOrDefault(c => c.Id == typeId);
                if (CycleType != null)
                {
                    CycleType.Name = typename;
                    CycleType.Price = price;
                    if (CycleParentId != 0)
                    {
                        CycleType.CycleBrandId = CycleParentId;
                    }
                    CycleModelService.Change(CycleType);
                }
            }
            else
            {
                CycleType.Name = typename;
                CycleType.CreateTime = DateTime.Now;
                CycleType.CycleBrandId = CycleParentId;
                CycleType.Price = price;
                CycleType.IsAbort = false;                             //修改完改回

                var last_sequence = CycleModelService.GetAll().Where(a => a.CycleBrandId == CycleParentId).OrderByDescending(a => a.Sequence);
                if (last_sequence.Count() > 0)
                {
                    CycleType.Sequence = last_sequence.FirstOrDefault().Sequence++;
                }
                else
                {
                    CycleType.Sequence = 1;
                }
                CycleModelService.Add(CycleType);
            }
            ShowMsg("操作成功", MessageItemType.Right);
            return Redirect("/admin/Cycle/editmodel?typeId=" + typeId + "&remain=" + CycleParentId);
        }

        public ActionResult DeleteModel()
        {
            var typeId = Query.Value("typeId").ToInt();
            if (typeId > 0)
            {
                CycleModelService.Remove(typeId);
                ShowMsg("操作成功!", MessageItemType.Right);
                return Redirect("/admin/Cycle/model");
            }
            ShowMsg("操作失败,参数不正确!", MessageItemType.Error);
            return Redirect("/admin/Cycle/model");
        }
        #endregion

        

        #region Problem维修故障
        public ActionResult Problem(int? mid)
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;

            var cities = CycleProblemService.GetAll().Where(a => a.IsAbort == false);

            if (mid.HasValue)
            {
                cities = cities.Where(a => a.CycleModelId == mid);
                ViewBag.mid = mid;
            }
            else
            {
                ViewBag.mid = 0;
            }

            if (key.IsNotNullOrEmpty())
            {
                cities = cities.Where(a => a.Name.Contains(key));
                ViewBag.Name = key;
            }
            var items = cities.OrderByDescending(u => u.CreateTime)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex).Select(a => new CycleProblem
                 {
                     Id = a.Id,
                     CreateTime = a.CreateTime,
                     Name = a.Name,
                     IsAbort = a.IsAbort,
                     CycleModel = a.CycleModel,
                     CycleModelId = a.CycleModelId,
                     CycleProblemDetail = a.CycleProblemDetail
                 });

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
           
            return View(items);
        }


        public ActionResult EditProblem()
        {
            var typeId = Query.Value("typeId").ToInt();
            var remain = Query.Value("remain").ToInt();
            ViewBag.remain = remain;
            var CycleType = new CycleProblem();
            if (typeId > 0)
            {
                CycleType = CycleProblemService.GetAll().FirstOrDefault(c => c.Id == typeId);
            }
            return View(CycleType);
        }

        [HttpPost]
        public ActionResult EditProblem(int? z)
        {
            var typename = Query.Value("typename");
            var typeId = Query.Value("typeId").ToInt();
            var CycleParentId = Query.Value("parentId").ToInt();  //父类id
            var CycleType = new CycleProblem();
            if (typeId > 0)
            {
                CycleType = CycleProblemService.GetAll().FirstOrDefault(c => c.Id == typeId);
                if (CycleType != null)
                {
                    CycleType.Name = typename;
                    if (CycleParentId != 0)
                    {
                        CycleType.CycleModelId = CycleParentId;
                    }
                    CycleProblemService.Change(CycleType);
                }
            }
            else
            {
                CycleType.Name = typename;
                CycleType.CreateTime = DateTime.Now;
                CycleType.CycleModelId = CycleParentId;
                CycleType.IsAbort = false;                             //修改完改回

                var last_sequence = CycleProblemService.GetAll().Where(a => a.CycleModelId == CycleParentId).OrderByDescending(a => a.Sequence);
                if (last_sequence.Count() > 0)
                {
                    CycleType.Sequence = last_sequence.FirstOrDefault().Sequence++;
                }
                else
                {
                    CycleType.Sequence = 1;
                }
                CycleProblemService.Add(CycleType);
            }
            ShowMsg("操作成功", MessageItemType.Right);
            return Redirect("/admin/Cycle/editProblem?typeId=" + typeId + "&remain=" + CycleParentId);
        }

        public ActionResult DeleteProblem()
        {
            var typeId = Query.Value("typeId").ToInt();
            if (typeId > 0)
            {
                CycleProblemService.Remove(typeId);
                ShowMsg("操作成功!", MessageItemType.Right);
                return Redirect("/admin/Cycle/Problem");
            }
            ShowMsg("操作失败,参数不正确!", MessageItemType.Error);
            return Redirect("/admin/Cycle/Problem");
        }
        #endregion

        #region ProblemDetail维修故障详情
        public ActionResult ProblemDetail(int? mid,int? pid)
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;

            var cities = CycleProblemDetailService.GetAll().Where(a => a.IsAbort == false);

            if (mid.HasValue)
            {
                cities = cities.Where(a => a.CycleProblem.CycleModelId == mid);
                if (pid.HasValue)
                {
                    cities = cities.Where(a => a.CycleProblemId == pid);
                    ViewBag.pid = pid;
                }
                else
                {
                    ViewBag.pid = 0;
                }
                ViewBag.mid = mid;
            }
            else
            {
                ViewBag.mid = 0;
            }

            if (key.IsNotNullOrEmpty())
            {
                cities = cities.Where(a => a.Name.Contains(key));
                ViewBag.Name = key;
            }
            var items = cities.OrderByDescending(u => u.CreateTime)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex).Select(a => new CycleProblemDetail
                 {
                     Id = a.Id,
                     CreateTime = a.CreateTime,
                     Name = a.Name,
                     IsAbort = a.IsAbort,
                     Price = a.Price,
                     CycleProblem = a.CycleProblem,
                     CycleProblemId = a.CycleProblemId
                 });

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
           
            return View(items);
        }


        public ActionResult EditProblemDetail()
        {
            var typeId = Query.Value("typeId").ToInt();
            var remain = Query.Value("remain").ToInt();
            ViewBag.remain = remain;
            var CycleType = new CycleProblemDetail();
            if (typeId > 0)
            {
                CycleType = CycleProblemDetailService.GetAll().FirstOrDefault(c => c.Id == typeId);
            }
            return View(CycleType);
        }

        [HttpPost]
        public ActionResult EditProblemDetail(int? z)
        {
            var typename = Query.Value("typename");
            var typeId = Query.Value("typeId").ToInt();
            var price = Query.Value("price").ToInt();
            var CycleParentId = Query.Value("parentId").ToInt();  //父类id
            var CycleType = new CycleProblemDetail();
            if (typeId > 0)
            {
                CycleType = CycleProblemDetailService.GetAll().FirstOrDefault(c => c.Id == typeId);
                if (CycleType != null)
                {
                    CycleType.Name = typename;
                    CycleType.Price = price;
                    if (CycleParentId != 0)
                    {
                        CycleType.CycleProblemId = CycleParentId;
                    }
                    CycleProblemDetailService.Change(CycleType);
                }
            }
            else
            {
                CycleType.Name = typename;
                CycleType.CreateTime = DateTime.Now;
                CycleType.Price = price;
                CycleType.CycleProblemId = CycleParentId;
                CycleType.IsAbort = false;                             //修改完改回

                var last_sequence = CycleProblemDetailService.GetAll().Where(a => a.CycleProblemId == CycleParentId).OrderByDescending(a => a.Sequence);
                if (last_sequence.Count() > 0)
                {
                    CycleType.Sequence = last_sequence.FirstOrDefault().Sequence++;
                }
                else
                {
                    CycleType.Sequence = 1;
                }
                CycleProblemDetailService.Add(CycleType);
            }
            ShowMsg("操作成功", MessageItemType.Right);
            return Redirect("/admin/Cycle/editProblemDetail?typeId=" + typeId + "&remain=" + CycleParentId);
        }

        public ActionResult DeleteProblemDetail()
        {
            var typeId = Query.Value("typeId").ToInt();
            if (typeId > 0)
            {
                CycleProblemDetailService.Remove(typeId);
                ShowMsg("操作成功!", MessageItemType.Right);
                return Redirect("/admin/Cycle/ProblemDetail");
            }
            ShowMsg("操作失败,参数不正确!", MessageItemType.Error);
            return Redirect("/admin/Cycle/ProblemDetail");
        }
        #endregion
    }
}